package com.example.suppliercontact.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.suppliercontact.pojo.Supplier;
import com.example.suppliercontact.pojo.SupplierContact;
import com.example.suppliercontact.service.SupplierContactService;
import com.example.suppliercontact.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.text.ParseException;
import java.util.List;

@Controller
public class SupplierContactController {
    @Autowired
    private SupplierContactService supplierContactService;

    @Autowired
    private SupplierService supplierService;

    /**
     * 查询
     * @param model
     * @return
     */
    @GetMapping("/list")
    public String list(Model model, SupplierContact supplierContact){

        QueryWrapper<SupplierContact> supplierContactQueryWrapper = new QueryWrapper<>();


        if (supplierContact.getPid() != null && supplierContact.getPid() != 0) {
            supplierContactQueryWrapper.eq("pid", supplierContact.getPid());
        }
        if (supplierContact.getCname() != null && supplierContact.getCname() != "") {
            supplierContactQueryWrapper.like("cname", supplierContact.getCname());
        }

        List<SupplierContact> list = supplierContactService.list(supplierContactQueryWrapper);
        List<Supplier> all = supplierService.list();

        model.addAttribute("list", list);
        model.addAttribute("all", all);
        return "main";//转发
    }


    /**
     * 新增
     * @param supplierContact
     * @return
     */
    @PostMapping("/add")
    public String add(SupplierContact supplierContact){
        boolean result = supplierContactService.save(supplierContact);

        if (result){
            return "redirect:list";//重定向
        }

        return "add";
    }

    @GetMapping("/toAdd")
    public String toAdd(Model model){

        List<Supplier> all = supplierService.list();

        model.addAttribute("all", all);

        return "add";
    }


    /**
     * 删除
     * @param id
     * @return
     */
    @PostMapping("/delete")
    @ResponseBody
    public boolean delete(Integer id){
        return supplierContactService.removeById(id);
    }


    /**
     * 查询单条记录
     * @param model
     * @param id
     * @return
     * @throws ParseException
     */
    @GetMapping("/byId")
    public String byId(Model model,Integer id) throws ParseException {

        SupplierContact supplierContact = supplierContactService.getById(id);

        List<Supplier> all = supplierService.list();

        model.addAttribute("all", all);

        model.addAttribute("supplierContact",supplierContact);

        return "update";
    }


    /**
     * 更新
     * @param supplierContact
     * @return
     */
    @PostMapping("/update")
    public String update(SupplierContact supplierContact) {

        boolean update = supplierContactService.updateById(supplierContact);

        if (update){
            return "redirect:list";//重定向
        }
        return "update";
    }
}
